#include "app.h"
#include <uis.h>
#include "../dbg/dbg.h"

// TODO: set previous state here
#define BACK_STATE			NEWAPP_STATE_MAIN

#define NAME_LEN			32


// -----------------------------------------------------------------------------------------
// Local constants



// -----------------------------------------------------------------------------------------
// Local functions

static UINT32 HandleBack( EVENT_STACK_T * ev_st, void * app );
static UINT32 HandleDone( EVENT_STACK_T * ev_st, void * app );
static UINT32 HandleData( EVENT_STACK_T * ev_st, void * app );


// -----------------------------------------------------------------------------------------
// Global handlers array

const EVENT_HANDLER_ENTRY_T chared_state_handlers[] =
{
	{ EV_CANCEL,				HandleBack },
	{ EV_DONE,					HandleDone },
	{ EV_DATA,					HandleData },

	{ STATE_HANDLERS_END,		NULL }
};

// -----------------------------------------------------------------------------------------
// Global enter/exit state functions

UINT32 CharEdStateEnter( EVENT_STACK_T * ev_st, void * app, ENTER_STATE_TYPE_T type )
{
	NEWAPP_T *		papp = newapp(app);
	SU_PORT_T		port = papp->apt.port;
	UIS_DIALOG_T	dialog;
	
	dbg("CharEdStateEnter: Enter, type = %d", type);
	if ( type != ENTER_STATE_ENTER )
		return RESULT_OK;
	
	// TODO: add your code here
	dialog =
	UIS_CreateCharacterEditor( &port, 
								NULL,	// text
								0x08,	// type
								NAME_LEN,		// str max length
								FALSE,	// masked, as pw
								NULL,
								LANG_NAME );
	dbg("CharEdStateEnter: UIS_CreateCharacterEditor DONE, dialog = %d", dialog);
	
	if ( dialog == 0 )
		return RESULT_FAIL;
	papp->apt.dialog = dialog;
	
	dbg("CharEdStateEnter: END", 0);
	return RESULT_OK;
}

UINT32 CharEdStateExit( EVENT_STACK_T * ev_st, void * app, EXIT_STATE_TYPE_T type )
{
	NEWAPP_T *		papp = newapp(app);
	UINT32			status;
	
	dbg("CharEdStateExit: Enter, type = %d", type);
	if ( type != EXIT_STATE_EXIT )
		return RESULT_OK;
	
	status = APP_UtilUISDialogDelete( &papp->apt.dialog );
	
	dbg("CharEdStateExit: END, status = %d", status);
	return status;
}


// -----------------------------------------------------------------------------------------
// Other local functions

UINT32 HandleBack( EVENT_STACK_T * ev_st, void * app )
{
	NEWAPP_T *		papp = newapp(app);
	UINT32			status;
	
	dbg("HandleBack: Enter", 0);
	APP_ConsumeEv(ev_st, app);
	
	// TODO: add your code here
	
	status = APP_UtilChangeState( BACK_STATE, ev_st, app );
	dbg("HandleBack: END, status = %d", status);
	return status;
}

UINT32 HandleDone( EVENT_STACK_T * ev_st, void * app )
{
	NEWAPP_T *		papp = newapp(app);
	EVENT_T *		event = AFW_GetEv(ev_st);
	UINT32			status;
	
	dbg("HandleBack: Enter", 0);
	APP_ConsumeEv(ev_st, app);
	
	// TODO: add your code here
	event->data.index = NAME_LEN;	// how many chars reqest from dialog
	status = AFW_AddEvEvD( ev_st, EV_REQUEST_DATA, (ADD_EVENT_DATA_T *)(&event->data_tag) );
	
	dbg("HandleBack: END, status = %d", status);
	return status;
}

UINT32 HandleData( EVENT_STACK_T * ev_st, void * app )
{
	NEWAPP_T *		papp = newapp(app);
	EVENT_T *		event = AFW_GetEv(ev_st);
	WCHAR *			str;
	
	dbg("HandleData: Enter", 0);
	APP_ConsumeEv( ev_st, app );
	
	// TODO: save the string
	str = event->attachment;
	udbg("HandleData: str is '%s'", str);
	
	dbg("HandleData: END", 0);
	return RESULT_OK;
}

